---@class Battle.CommonAi : Battle.AiBase
local CommonAi = Battle.CreateClass("CommonAi",Battle.AiBase)
Battle.CommonAi = CommonAi

---@param actor Battle.Entity
local function GetHandFixPos(actor)
    return actor.aiSystem:GetBlackboardValue("hand_pos")
end
---@param actor Battle.Entity
local function GetTarget(actor)
    return actor:GetTarget()
end
---@param actor Battle.Entity
local function GetSkill(actor)
    return actor.skillSystem:GetSkill1Id()
end
---@param actor Battle.Entity
local function GetBornTime(actor)
    return actor.aiSystem:GetBlackboardValue("bornTime")
end
---@param actor Battle.Entity
local function SearchTarget(actor)
    local enemyCamp = Battle.BattleUtil.GetEnemyCamp(actor.camp)
    local skill = actor.skillSystem:GetNormalSkill()
    if not skill then return nil end
    local target =  skill.emitter:GetSearchTarget(actor.searchRadius)
    if target == nil then
        target = Battle.ActorManager.GetLord(enemyCamp)
    end
    return target
end

function CommonAi:OnCreate()
    local Tree = Battle.AiTree
    local Decorator = Battle.AiDecoratorType
    local Condition = Battle.AiConditionType
    local Action = Battle.AiActionType

    Tree.CLEAR()
    --循环
    Tree.BEGIN_DECORATOR(Decorator.Repeater,-1)
--[[--]]Tree.BEGIN_SELECTOR()
            --出生
    --[[--]]Tree.BEGIN_CONDITION(Condition.Trigger,"isBorn",true)
    --[[    --]]Tree.BEGIN_SEQUENCE()
    --[[        --]]Tree.ADD_ACTION(Action.Wait,GetBornTime)
    --[[        --]]Tree.ADD_ACTION(Action.RemoveEffectState,Battle.FighterStateEnum.CAN_NOT_SELECT_BY_ENEMY)
    --[[        --]]Tree.ADD_ACTION(Action.TriggerSet,"isBorn",false)
    --[[    --]]Tree.END_SEQUENCE()
    --[[--]]Tree.END_CONDITION()
    --眩晕
    --[[--]]Tree.BEGIN_CONDITION(Condition.Trigger,"stun",true)
    --[[    --]]Tree.BEGIN_SEQUENCE()
    --[[        --]]Tree.ADD_ACTION(Action.EnterStunAction)
    --[[    --]]Tree.END_SEQUENCE()
    --[[--]]Tree.END_CONDITION()
    --受击表现
    --[[--]]Tree.BEGIN_CONDITION(Condition.Trigger,"hit",true)
    --[[    --]]Tree.BEGIN_SEQUENCE()
    --[[        --]]Tree.ADD_ACTION(Action.EnterHitAction)
    --[[        --]]Tree.ADD_ACTION(Action.TriggerSet,"hit",false)
    --[[    --]]Tree.END_SEQUENCE()
    --[[--]]Tree.END_CONDITION()
            --手动释放技能
    --[[--]]Tree.BEGIN_CONDITION(Condition.Trigger,"hand_skill",true)
    --[[    --]]Tree.BEGIN_SELECTOR()
    --[[        --]]Tree.BEGIN_CONDITION(Condition.CanUseSkill,self.actor.skillSystem:GetSkill1Id())
    --[[            --]]Tree.BEGIN_SEQUENCE()
    --[[                --]]Tree.ADD_ACTION(Action.DoSkill,GetSkill)
    --[[                --]]Tree.ADD_ACTION(Action.TriggerSet,"hand_skill",false)
    --[[            --]]Tree.END_SEQUENCE()
    --[[        --]]Tree.END_CONDITION()
    --[[    --]]Tree.ADD_ACTION(Action.TriggerSet,"hand_skill",false)
    --[[    --]]Tree.END_SELECTOR()
    --[[--]]Tree.END_CONDITION()
    --手动移动
    --[[--]]Tree.BEGIN_CONDITION(Condition.Trigger,"hand_move",true)
    --[[    --]]Tree.BEGIN_CONDITION(Condition.CanMove)
    --[[        --]]Tree.BEGIN_SEQUENCE()
    --[[            --]]Tree.ADD_ACTION(Action.PathFinding,true,GetHandFixPos)
    --[[            --]]Tree.ADD_ACTION(Action.TriggerSet,"hand_move",false)
    --[[        --]]Tree.END_SEQUENCE()
    --[[    --]]Tree.END_CONDITION()
    --[[--]]Tree.END_CONDITION()
            --攻击目标
    --[[--]]Tree.BEGIN_CONDITION(Condition.HasTarget)--有目标
    --[[    --]]Tree.BEGIN_SELECTOR()
    --[[        --]]Tree.BEGIN_CONDITION(Condition.InSkillRange,self.actor.skillSystem:GetNormalSkillId())--在技能范围类
    --[[            --]]Tree.ADD_ACTION(Action.DoSkill,self.actor.skillSystem:GetNormalSkillId())
    --[[        --]]Tree.END_CONDITION()

    --[[        --]]Tree.BEGIN_SEQUENCE()
    --[[            --]]Tree.BEGIN_CONDITION(Condition.Trigger,"attack_target",false)--非指定攻击特定目标
    --[[                --]]Tree.ADD_ACTION(Action.SetSearchTarget,SearchTarget)
    --[[            --]]Tree.END_CONDITION()
    --[[            --]]Tree.BEGIN_CONDITION(Condition.CanPathFinding)
    --[[                --]]Tree.ADD_ACTION(Action.PathFinding,false,GetTarget)
    --[[            --]]Tree.END_CONDITION()
    --[[        --]]Tree.END_SEQUENCE()

    --[[    --]]Tree.END_SELECTOR()
    --[[--]]Tree.END_CONDITION()
            --搜索敌人
    --[[--]]Tree.BEGIN_SEQUENCE()
    --[[    --]]Tree.ADD_ACTION(Action.TriggerSet,"attack_target",false)
    --[[    --]]Tree.ADD_ACTION(Action.SetSearchTarget,SearchTarget)
    --[[    --]]Tree.BEGIN_CONDITION(Condition.CanPathFinding)
    --[[        --]]Tree.ADD_ACTION(Action.PathFinding,false,GetTarget)
    --[[    --]]Tree.END_CONDITION()
    --[[--]]Tree.END_SEQUENCE()
--[[--]]Tree.END_SELECTOR()
    Tree.END_DECORATOR()

    self.tree = Tree.New(Tree.GET_ROOT(),self.actor)
    self.tree:Start()
    --出生等待
    self.actor.effectStateSystem:AddState(Battle.FighterStateEnum.CAN_NOT_SELECT_BY_ENEMY)
    local heroCfg = DataTable.GetById("t_hero",self.actor.modelId)
    self.tree.blackboard:Set("bornTime",heroCfg.f_CallDelayed/1000)
    self.tree.blackboard:Set("isBorn",true)
end
